package main;
import java.io.*;
import java.util.Properties;



public class DataIO {
	private int dimensions;
	private int numberOfParticles;
	
	private double[][] positions;
	private double[][] velocities;
	private double[] masses;
	private double[] charges;

    int particleNo = 0;
	
	public Particles readFromPropertiesFile(String inputName){
		// method parameters

		
		
	 	File input = new File(inputName);
	 	boolean display = false;
		
		
		 try {	
			 	
			 	
			 	// loads properties file
			 	Properties properties = new Properties();
		        properties.load(new FileInputStream(input));
		        this.numberOfParticles = Integer.parseInt(properties.getProperty("numberOfParticles"));
		        this.dimensions = Integer.parseInt(properties.getProperty("dimensions"));
		        particleNo = 0;
		        
		        this.positions = new double[this.numberOfParticles][this.dimensions];
				this.velocities = new double[this.numberOfParticles][this.dimensions];
				this.masses = new double[this.numberOfParticles];
				this.charges= new double[this.numberOfParticles];

		       for(int i = 0; i < numberOfParticles; i++){
		        	String currentParticle = null;
		        	currentParticle =  properties.getProperty(Integer.toString(i));
		        	setParticle(currentParticle);
		        	this.particleNo ++;
		       	}
		       
		       
		       //sets up particles object
		       Particles particles = new Particles(this.dimensions,this.numberOfParticles,this.positions,this.velocities,this.charges,this.masses);
		        return particles;
		    } catch (IOException e) {System.out.println("IO problem"+e); 
		    	Particles particles = new Particles();
		    	return particles;}

	}

	private void setParticle(String currentParticle) {
		
		String[] attributes = currentParticle.split(",");
		
		for(int i = 0 ; i<dimensions;i++){
			this.positions[this.particleNo][i] = Double.parseDouble(attributes[i]);
			}
		
		for(int i = 0 ; i<dimensions;i++){
			this.velocities[this.particleNo][i] = Double.parseDouble(attributes[i+this.dimensions]);
			}
		
		this.charges[particleNo] = Double.parseDouble(attributes[(2*dimensions)]);
		this.masses[particleNo] = Double.parseDouble(attributes[(2*dimensions)+1]);	
	}
}
